import csv,sys,re

from utils import distance
from utils import ftype_pos

assert sys.argv[1] != sys.argv[4]

listreader = csv.reader(open(sys.argv[1],'r'),delimiter='\t')
listwriter = csv.writer(open(sys.argv[4],'w'),delimiter='\t')

for line in listreader:
    target,feature,pvalue,importance,correlation = line

    target_elems = re.split(':',target)
    feature_elems = re.split(':',feature)

    if target_elems[ftype_pos] == sys.argv[2] and feature_elems[ftype_pos] == sys.argv[3]:
        listwriter.writerow([distance(target_elems,feature_elems),pvalue,importance,correlation])
